package com.acce.base.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.acce.base.pojo.ArgsPojo;
import com.alibaba.fastjson.JSONObject;

public class BaseController {
	
		protected Logger logger = Logger.getLogger(this.getClass());
		
		
		protected void exceptionWriteReturn(String serviceCode, String returnCode, String returnMsg, String cacheTime, String checkTime,HttpServletResponse response){
			JSONObject json = new JSONObject();
			json.put("serviceCode", serviceCode);
			json.put("returnCode", returnCode);
			json.put("returnMsg", returnMsg);
			json.put("cacheTime", cacheTime);
			json.put("checkTime", checkTime);
			writeJsonToResponse(response, json.toJSONString());
		}

		/**
		 * 向客户端输出
		 * @param response
		 * @param json
		 */
		protected void writeJsonToResponse(HttpServletResponse response,String json) {
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter writer = null;
			try {
				writer = response.getWriter();
				writer.println(json);
				writer.flush();
			} catch (IOException e) {
				logger.error("acce-base::writeJsonToResponse::输出异常"+json,e);
			} finally {
				if(null != writer) {
					writer.close();
				}
			}
		}
		
		protected boolean isEmptyHead(ArgsPojo args){
			if(StringUtils.isEmpty(args.getServiceCode()) || StringUtils.isEmpty(args.getSign()) || StringUtils.isEmpty(args.getTimestamp()) 
					||StringUtils.isEmpty(args.getNonce()) || StringUtils.isEmpty(args.getEditUserId()) || StringUtils.isEmpty(args.getEditTime())
					|| StringUtils.isEmpty(args.getDevice()) || StringUtils.isEmpty(args.getIpaddr()) || StringUtils.isEmpty(args.getMac())){
				return true;
			}else{
				return false;
			}
		}
}
